package com.shr25.robot.qq.plugins;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.shr25.robot.qq.model.QqMessage;
import com.shr25.robot.qq.util.MessageUtil;
import net.mamoe.mirai.message.data.Image;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 涩图片插件
 *
 * @author huobing
 * @date 2022/6/3 21:25
 */
@Component
public class GeekImagePlugin extends RobotPlugin {
    public static boolean state = true;

    public GeekImagePlugin() {
        super();
        setName("涩图片插件");
        addDescLn("涩图片插件");
        addCommand("戒色图|js|jie", "查询戒色图");
        setSort(1);
    }


    @Override
    public boolean executeMessage(QqMessage qqMessage) {
        String content = qqMessage.getContent();
        if (content.matches("戒色图||js||jie")) {
            this.jiesetu(qqMessage);
            return false;
        }
        if (content.indexOf("张戒色图") != -1) {
            if (!qqMessage.isRoot()) {
                qqMessage.putReplyMessage("你不是美美的主人");
                return false;
            }

            String regEx="[^0-9]";
            Pattern p = Pattern.compile(regEx);
            Matcher str = p.matcher(content);
            int num = Integer.parseInt(str.replaceAll(""));
            for (int i=0; i<num; i++) {
                this.jiesetu(qqMessage);
            }
            return false;
        }

        if (content.indexOf("搜图") != -1) {
            String keyword = content.replace("搜图", "");
            this.soutu(qqMessage, keyword);
        }

        return true;
    }

    private void sendImagesByUrl(QqMessage qqMessage, String url) {
        HttpResponse response = HttpRequest.get(url).setFollowRedirects(true).execute();
        Image image = MessageUtil.buildImageMessage(qqMessage.getContact(), response.bodyStream());
        if (image != null) {
            qqMessage.putReplyMessage(image);
        }
    }

    /**
     * 百度soutu
     * @return
     */
    private void soutu(QqMessage qqMessage, String keyword) {
        JSONObject res = JSONUtil.parseObj(
                HttpRequest.get("https://image.baidu.com/search/acjson?tn=resultjson_com&word=" + keyword).header(
                        Header.COOKIE, "BIDUPSID=79551127DE4C876B4DDDCF3AFFB0EDAD; PSTM=1643457684; BAIDUID=DDC6DE5CCE3FB2F96338630E8E21D11C:FG=1; BDUSS=tybzBVNVNLeHJ2QUZvTmRFMHo4aG1EUWhvY0tWMUcyY1VSWk9vTmhsZ3NYOU5pSVFBQUFBJCQAAAAAAAAAAAEAAAA9e5Gvc29sZGllcnNlbwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACzSq2Is0qtiYm; BDUSS_BFESS=tybzBVNVNLeHJ2QUZvTmRFMHo4aG1EUWhvY0tWMUcyY1VSWk9vTmhsZ3NYOU5pSVFBQUFBJCQAAAAAAAAAAAEAAAA9e5Gvc29sZGllcnNlbwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACzSq2Is0qtiYm; H_WISE_SIDS=110085_127969_179347_180636_188749_189755_190623_194085_194519_194529_196427_196528_197711_199582_204901_205414_206123_208721_208809_209568_210303_210321_210443_210969_211433_211953_212295_212416_212533_212846_213045_213079_213278_213362_213868_214094_214205_214596_214642_214792_215070_215127_215281_215555_215727_215859_215892_215960_216253_216297_216333_216342_216354_216450_216596_216646_216837_216943_217048_217184_217207_217388_217392_217454_217761_218033_218100_218326_218390_218394_218444_218456_218463_218538_218549_218597_218857_218957_218966_218992_219240_219245_219255_219329; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; H_PS_PSSID=36561_36625_36721_36455_31254_36452_36690_36165_36694_36698_36074_36772_36745_36763_36770_36767_26350_36469_36712; BAIDUID_BFESS=60E9500B293D87D27B80450A6A1E8D31:FG=1; BDRCVFR[X_XKQks0S63]=mk3SLVN4HKm; firstShowTip=1; cleanHistoryStatus=0; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; indexPageSugList=[\"美女\",\"1\",\"window logo\",\"edge logo\",\"java logo\",\"百度浏览器该页面存在木马病毒\",\"电脑图形\",\"教室图形\",\"拼多多订单核销\"]"

                ).timeout(20000).execute().body()

        );
        JSONArray result = res.getJSONArray("data");
        Random random = new Random();
        int num = random.nextInt(result.size());
        JSONObject jsonArray = result.getJSONObject(num);
        this.sendImagesByUrl(qqMessage, jsonArray.getStr("thumbURL"));
    }

    /**
     * 戒色图
     * @return
     */

    private void jiesetu(QqMessage qqMessage) {
        String root = "https://cdn.jieseba.com/tu/";
        Random random = new Random();
        int num = random.nextInt(10000);
        this.sendImagesByUrl(qqMessage, root + String.valueOf(num) + ".jpg");
    }
}
